package com.igroupes.ovineherd.model.Style;

import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class RadioStyle extends BasicStyle {

    public RadioStyle(){}
    private RadioStyle(String label, Object value, List<Group> group) {
        super(label, value);
        this.type = FormKey.RADIO.getCode();
        this.options = group;
    }
    public RadioStyle(String label, Object value, List<Group> group, String desc) {
        this(label, value, group);
        this.desc = desc;
    }

    protected List<Group> options;

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof RadioStyle)){
            throw new ModelException("校验style类类型错误");
        }
        RadioStyle style = (RadioStyle) o;
        if(!style.getDisabled()){
            super.verify(o);
            Integer check = 1;
            for (Object g :
                    style.getOptions()) {
                if(!(g instanceof Group)){
                    throw new ModelException(style.getLabel()+"传值类型错误");
                }
                Group group1 = (Group) g;
                if(!group1.getDisabled() && group1.getChecked()){
                    check++;
                }
                group1.verify(group1);
            }
            if(1 != check){
                throw new ModelException(style.getLabel()+"选中值不能为空且只能选中一个");
            }
        }
        return true;
    }

    public Object getCheck(Object o) {
        RadioStyle style = (RadioStyle) o;
        if(!style.getDisabled()){
            for (Object g :
                    style.getOptions()) {
                Group group1 = (Group) g;
                if(!group1.getDisabled() && group1.getChecked()){
                    return group1.getValue();
                }
            }
        }
        return "";
    }
}
